//该属性用于隐藏对未使用代码的警告
#![allow(dead_code)]

enum WebEvent{
    PageLoad,
    PageUnload,
    KeyPress(char),
    Paste(String),
    Click {x: i64,y:i64}
}
struct PointPos {
    x:f32,
    y:f32
}

//此函数将一个'WebEvent' enum作为参数
fn inspect(event:WebEvent) {
    match  event{
        WebEvent::PageLoad => println!("page loaded"),
        WebEvent::PageUnload => println!("page unloaded"),
        // 从 `enum` 里解构出 `c`。
        WebEvent::KeyPress(c) => println!("pressed '{}'.", c),
        WebEvent::Paste(s) => println!("pasted \"{}\".", s),
        WebEvent::Click {x,y} => {
            println!("clicked at x={}, y={}.", x, y);
        }
    }
}

enum VeryVerboseEnumofThingsToDoWithNumbers {
    Add,
    Subtract,
}

//枚举名过长，可用type关键字改名
type Operations = VeryVerboseEnumofThingsToDoWithNumbers;

impl Operations {
    fn run(&self,x:i32,y:i32) -> i32 {
        match self {
            Self::Add => x+y,
            Self::Subtract => x-y,
        }
    }
}

pub fn example() {
    let mut pos1 = PointPos{
        x: 50f32,y: 50f32
    };

    let pressed = WebEvent::KeyPress('x');
    // `to_owned()` 从一个字符串切片中创建一个具有所有权的 `String`。
    let pasted  = WebEvent::Paste("my text".to_owned());
    let click   = WebEvent::Click { x: 20, y: 80 };
    let load    = WebEvent::PageLoad;
    let unload  = WebEvent::PageUnload;

    inspect(pressed);
    inspect(pasted);
    inspect(click);
    inspect(load);
    inspect(unload);

    println!("hello world0")
}

//如果
